package gentle.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import gentle.base.SqlMapper;
import gentle.base.StringUtil;
import gentle.entity.*;
import gentle.mapper.MemberProductDao;
import gentle.service.CustomerForGoodsService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CustomerForGoodsServiceImpl implements CustomerForGoodsService {

    @Autowired
    private SqlMapper sqlMapper;

    @Autowired
    private MemberProductDao dao;

    @Override
    public List<PointRewordInfo> selectPageListByQueryGetMap(Page<PointRewordInfo> page, SearchPointRewordInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getStatus())) {
            params.put("status", searchInfo.getStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getPhone())) {
            params.put("phone", searchInfo.getPhone());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartCreateTime())) {
            params.put("startCreateTime", searchInfo.getStartCreateTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndCreateTime())) {
            params.put("endCreateTime", searchInfo.getEndCreateTime());
        }
        params.put("startIndex", searchInfo.getStartIndex());
        params.put("limitIndex", searchInfo.getLimitIndex());
        return dao.getQueryPage(page, params);
    }

    @Override
    public int getQueryPagesMemberCount(Page<PointRewordInfo> page, SearchPointRewordInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getStatus())) {
            params.put("status", searchInfo.getStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getPhone())) {
            params.put("phone", searchInfo.getPhone());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartCreateTime())) {
            params.put("startCreateTime", searchInfo.getStartCreateTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndCreateTime())) {
            params.put("endCreateTime", searchInfo.getEndCreateTime());
        }
        return dao.getQueryPagesMemberCounts(page, params);
    }

    public List<PointRewordInfo> PointRewordInfototal(String startCreateTime, String endCreateTime) {
        String contitionInfo = "";
        if (StringUtils.isNotBlank(startCreateTime)) {
            contitionInfo += " and t.create_time >='" + startCreateTime + "' ";
        }
        if (StringUtils.isNotBlank(endCreateTime)) {
            contitionInfo += " and t.create_time <='" + endCreateTime + "' ";
        }
        String sql = " select count(*) as countNum from (select t.id from  point_conversion_info t  where t.status ='1' " + contitionInfo + ") t" +

                " union all  select count(*) as countNum from (select t.id from  point_conversion_info t where t.status ='2' " + contitionInfo + ") t";

        List<PointRewordInfo> tongJiInfoList1 = sqlMapper.selectList(sql, PointRewordInfo.class);
        return tongJiInfoList1;
    }
}